<template>
	<view class="public-card">
		<u-cell-item :title="bigTitle" :titleStyle="titleStyle" :arrow="false">
		</u-cell-item>
		<block v-for="(val,key) in  forKey" :key="key">
			<u-cell-item :title="forKey[key].title" :valueStyle="{...valueStyle,color:forKey[key].color}"
				:titleStyle="itemTitleStyle" :value="forKey[key].click?'':order[key]" :arrow="false">
				<block v-for="(item ,index) in  order[key]" :key="index">
					<view v-if="key==='pdffile'" @tap="saveFile(item)">
						{{forKey[key].title}}
					</view>
				</block>
			</u-cell-item>
		</block>
	</view>
</template>

<script>
	export default {
		options: {
			styleIsolation: 'shared', // 解除样式隔离
		},
		name: "publicCard",
		props: {
			bigTitle: '',
			order: {
				type: Object,
				default: () => {}
			},
			forKey: {
				type: Object,
				default: () => {}
			},
		},
		data() {
			return {
				titleStyle: {
					fontSize: '29rpx',
					fontWeight: 'bold',
					color: '#333333',
				},
				itemTitleStyle: {
					fontSize: '25rpx',
					color: '#999999',
					width: '150rpx'
				},
				valueStyle: {
					fontSize: '25rpx',
					color: '#333333',
					whiteSpace: 'pre-wrap'
				}
			}
		},
		methods: {
			saveFile(file) {
				// #ifdef MP-WEIXIN
				uni.showLoading({
					title: '下载中'
				});
				//下载文件
				uni.downloadFile({ //只能是GET请求
					url: file, //请求地址(后台返回的码流地址)
					success: (res) => {
						//下载成功
						if (res.statusCode === 200) {
							//保存文件
							let tempFile = res.tempFilePath;
							//保存成功之后 打开文件
							uni.getFileSystemManager().saveFile({
								tempFilePath: tempFile,
								// filePath: wx.env.USER_DATA_PATH + '/' + '上传成员.pdf',//自定义文件名
								success(res) {
									console.log(res)
									uni.openDocument({
										filePath: res.savedFilePath,
										showMenu: true, //是否可以分享
										success: (res) => {
											uni.hideLoading()
											console.log(res);
										},
										fail: (e) => {
											uni.showToast({
												title: '打开失败',
												icon: "error"
											})
										}
									})

								}
							})
						}
					},
					fail: (e) => {
						uni.showToast({
							title: '文件下载失败',
							icon: "error",
						})
					}
				});
				// #endif 
				// #ifdef H5
				let a = document.createElement('a')
				let event = new MouseEvent('click')
				a.download = ''
				a.href = file
				a.target = '_blank'
				a.dispatchEvent(event)

				// document.removeChild(a)
				// console.log(file)
				// #endif
			}
		}
	}
</script>

<style scoped lang="scss">
	.public-card {
		padding: 0 30rpx !important;
		margin-top: 15rpx;
		background-color: #fff;
		border-radius: 10rpx;
	}

	/deep/.u-cell {
		padding: 26rpx 0;
	}

	/deep/.u-cell-hover {
		background-color: transparent !important;
	}
</style>